package com.yoooya.base.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;

import com.yoooya.base.model.bo.JSONTreeNode;

public class TreeModelUtils {

	public static List<JSONTreeNode> buildTreeModel(List<JSONTreeNode> lists) {
		int index = 0;
		List<JSONTreeNode> tempNodes = new ArrayList<JSONTreeNode>();
		for (; index < lists.size();) {
			JSONTreeNode root = lists.get(index);
			if (root.getParent() == null || root.getParent() == 0) {
				lists.remove(index);
				index = 0;
				root.setChildren(createTreeChildren(lists, root.getId()));
				tempNodes.add(root);
			} else {
				index++;
			}
		}
		return tempNodes;
	}

	public static List<JSONTreeNode> createTreeChildren(
			List<JSONTreeNode> nodes, Long parentId) {
		List<JSONTreeNode> childrens = new ArrayList<JSONTreeNode>();
		int index = 0;
		for (; index < nodes.size();) {
			JSONTreeNode children = nodes.get(index);
			if (children.getParent() != null
					&& children.getParent().longValue() == parentId.longValue()) {
				nodes.remove(index);
				index = 0;
				children.setChildren(createTreeChildren(nodes, children.getId()));
				childrens.add(children);
			} else {
				index++;
			}
		}
		return childrens;
	}

	public static List<Map<String, Object>> buildMapTreeModel(
			List<Map<String, Object>> lists) {
		int index = 0;
		List<Map<String, Object>> tempNodes = new ArrayList<Map<String, Object>>();
		for (; index < lists.size();) {
			Map<String, Object> root = lists.get(index);
			Long parentId = (Long) root.get("parentId");
			if (parentId == null || parentId == 0) {
				lists.remove(index);
				index = 0;
				List<Map<String, Object>> data = createMapTreeChildren(lists,
						(Long) root.get("id"));
				if (CollectionUtils.isNotEmpty(data)) {
					root.put("leaf", false);
				} else {
					root.put("leaf", true);
				}
				root.put("expanded", true);
				root.put("data", data);
				tempNodes.add(root);
			} else {
				index++;
			}
		}
		return tempNodes;
	}

	public static List<Map<String, Object>> createMapTreeChildren(
			List<Map<String, Object>> nodes, Long parentId) {
		List<Map<String, Object>> childrens = new ArrayList<Map<String, Object>>();
		int index = 0;
		for (; index < nodes.size();) {
			Map<String, Object> children = nodes.get(index);
			Long subParentId = (Long) children.get("parentId");
			if (subParentId != null
					&& subParentId.longValue() == parentId.longValue()) {
				nodes.remove(index);
				index = 0;
				List<Map<String, Object>> data = createMapTreeChildren(nodes,
						(Long) children.get("id"));
				if (CollectionUtils.isNotEmpty(data)) {
					children.put("leaf", false);
				} else {
					children.put("leaf", true);
				}
				children.put("data", data);
				children.put("expanded", true);
				childrens.add(children);
			} else {
				index++;
			}
		}
		return childrens;
	}
	
	

	public static List<Map<String, Object>> buildUserTreeModel(
			List<Map<String, Object>> lists) {
		int index = 0;
		List<Map<String, Object>> tempNodes = new ArrayList<Map<String, Object>>();
		for (; index < lists.size();) {
			Map<String, Object> root = lists.get(index);
			Long parentId = (Long) root.get("parentId");
			if (parentId == null || parentId == 0) {
				lists.remove(index);
				index = 0;
				List<Map<String, Object>> data = createUserTreeChildren(lists,
						(Long) root.get("id"));
				if (CollectionUtils.isNotEmpty(data)) {
					root.put("leaf", false);
				} else {
					root.put("leaf", true);
				}
				root.put("expanded", true);
				root.put("data", data);
				tempNodes.add(root);
			} else {
				index++;
			}
		}
		return tempNodes;
	}

	public static List<Map<String, Object>> createUserTreeChildren(
			List<Map<String, Object>> nodes, Long parentId) {
		List<Map<String, Object>> childrens = new ArrayList<Map<String, Object>>();
		int index = 0;
		for (; index < nodes.size();) {
			Map<String, Object> children = nodes.get(index);
			Long subParentId = (Long) children.get("parentId");
			if (subParentId != null
					&& subParentId.longValue() == parentId.longValue()) {
				nodes.remove(index);
				index = 0;
				List<Map<String, Object>> data = createUserTreeChildren(nodes,
						(Long) children.get("id"));
				if (CollectionUtils.isNotEmpty(data)) {
					children.put("leaf", false);
				} else {
					children.put("leaf", true);
				}
				children.put("data", data);
				//children.put("expanded", true);
				childrens.add(children);
			} else {
				index++;
			}
		}
		return childrens;
	}
}
